﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace SmartSpace.Silverlight
{
    /// <summary>
    /// 应用模块信息
    /// </summary>
    public abstract class AppModule
    {
        /// <summary>
        /// 模块编号
        /// </summary>
        public Guid ID { get; private set; }

        /// <summary>
        /// 模块名称
        /// </summary>
        public string Name { get; private set; }

        public Dictionary<string, object> Settings { get; private set; }

        protected AppModule(Guid id, string name)
        {
            this.ID = id;
            this.Name = name;
        }

        public void LoadSettings(Dictionary<string, object> settings)
        {
            this.Settings = settings;
        }

        public static T GetInstance<T>() where T : AppModule
        {
            AppModule module = AppModuleManager.GetModule(typeof(T));
            if (module != null)
            {
                return (T)module;
            }
            return null;
        }

        public AppView CreateView(string name)
        {
            Assembly assembly = this.GetType().Assembly;
            Type[] types = assembly.GetTypes();

            foreach (Type type in types)
            {
                if (type.IsSubclassOf(typeof(AppView)) && type.FullName == name)
                {
                    object viewObject = assembly.CreateInstance(type.FullName);
                    if (viewObject != null)
                    {
                        return (AppView)viewObject;
                    }
                }
            }
            return null;
        }

        List<AppView> m_appViews = null;
        public AppView GetAppView(int viewID)
        {
            if (m_appViews == null)
            {
                m_appViews = new List<AppView>();
                Assembly assembly = this.GetType().Assembly;
                Type[] types = assembly.GetTypes();

                foreach (Type type in types)
                {
                    if (type.IsSubclassOf(typeof(AppView)))
                    {
                        object viewObject = assembly.CreateInstance(type.FullName);
                        if (viewObject != null)
                        {
                            m_appViews.Add((AppView)viewObject);
                        }
                    }
                }
            }

            foreach (AppView appView in m_appViews)
            {
                if (appView.ViewID == viewID)
                {
                    return appView;
                }
            }
            return null;
        }

        /// <summary>
        /// 切换到子试图
        /// </summary>
        public virtual void SwitchSubView(SwitchArgument arg)
        {
        }
    }

    public class SwitchArgument
    {
        public SlView ParentView{get;set;}
        public int ViewID{get;set;}
        public int RecordID{get;set;}
        public bool IsFinishedJob{get;set;}
        public ViewHideEventHandler HideEvent { get; set; }
        public string TableName { get; set; }

        public SwitchArgument(SlView parentView,
            int viewID,
            int recordID,
            bool isFinishedJob,
            string tableName,
            ViewHideEventHandler hideEvent)
        {
            this.ParentView = parentView;
            this.ViewID = viewID;
            this.RecordID = recordID;
            this.IsFinishedJob = isFinishedJob;
            this.HideEvent = hideEvent;

            this.TableName = tableName;
        }
    }
}
